package com.example.seckill.Controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.example.seckill.annotation.CurrentUser;
import com.example.seckill.common.CommonResult;

import com.example.seckill.mapper.PermissionMapper;

import com.example.seckill.mapper.RoleMapper;
import com.example.seckill.pojo.Permission;
import com.example.seckill.pojo.RolePermission;
import com.example.seckill.pojo.User;
import com.example.seckill.pojo.UserRole;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Resource
    PermissionMapper permissionMapper;

    @Resource
    RoleMapper roleMapper;

    @PostMapping
    public CommonResult<?> save(@RequestBody Permission Permission) {
        permissionMapper.insert(Permission);
        return CommonResult.success(Permission);
    }

    @PutMapping
    public CommonResult<?> update(@RequestBody Permission Permission) {
        permissionMapper.updateById(Permission);
        return CommonResult.success(Permission);
    }

    @DeleteMapping("/{id}")
    public CommonResult<?> update(@PathVariable Long id) {

        permissionMapper.deleteById(id);
        return CommonResult.success(id);
    }

    @GetMapping("{id}")
    public CommonResult<?> getById(@PathVariable Long id) {
        return CommonResult.success(permissionMapper.selectById(id));
    }

    @GetMapping("/all")
    public CommonResult<?> all() {
        return CommonResult.success(permissionMapper.selectList(null));
    }

    @GetMapping
    public CommonResult<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<Permission> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Permission::getName, search);
        }
        Page<Permission> PermissionPage = permissionMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return CommonResult.success(PermissionPage);
    }




//        }
//        // 对资源根据id进行排序
//        LinkedHashSet<Permission> sortedSet = permissionSet.stream().sorted(Comparator.comparing(Permission::getId)).collect(Collectors.toCollection(LinkedHashSet::new));
//        //设置当前用户的资源信息
//        res.setPermissions(sortedSet);
//
    }

